﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using GestionPedago.EntityContext;
using GestionPedago.EntityContext.EntityOPTools;
using GestionPedago.Persistence.OPManager;


namespace Persistence.OPManager
{
    /// <summary>
    /// Manager de persistance de l'Entity Appreciation
    /// </summary>
    public abstract class AppreciationOPManager
    {


        /// <summary>
        /// Méthode de récupération de la liste des Appreciations par PlanningIndividuelFormation
        /// </summary>
        /// <param name="pif">PlanningIndividuelFormation concerné</param>
        /// <returns>La liste des Appreciations</returns>
        public static List<Appreciation> GetListeAppreciationByPlanningIndividuelFormation(PlanningIndividuelFormation pif)
        {
            try
            {
                IQueryable<Appreciation> query = EntityConnection.GetContext().Appreciation;

                query = query.Where(it => it.CodePlanning == pif.CodePlanning);

                return query.ToList();

            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Méthode de récupération d'un Appreciation par son identifiant
        /// </summary>
        /// <param name="id">Identifiant de l'Appreciation à récupérer</param>
        /// <returns>L'Appreciation</returns>
        public static Appreciation GetAppreciationById(long id)
        {

            try
            {
                var query = from it in EntityConnection.GetContext().Appreciation
                            where it.IdAppreciation == id
                            select it;

                Appreciation e = null;

                e = query.First();

                return e;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// Méthode d'ajout d'un Appreciation dans la base
        /// </summary>
        /// <param name="Appreciation">L'Appreciation à ajouter en base</param>
        /// <returns>L'Appreciation</returns>
        public static Appreciation AjouterAppreciation(Appreciation appreciation)
        {

            try
            {
                if(appreciation.ContactENI != null) {
                    EntityConnection.GetContext().ContactENI.Detach(appreciation.ContactENI);
                }
                if(appreciation.Cours != null) {
                    EntityConnection.GetContext().Cours.Detach(appreciation.Cours);
                }

                if(appreciation.PlanningIndividuelFormation != null) {
                    EntityConnection.GetContext().PlanningIndividuelFormation.Detach(appreciation.PlanningIndividuelFormation);
                }

                if (!EntityStateChecker.IsDetached(appreciation))
                {
                    EntityConnection.GetContext().Appreciation.Detach(appreciation);
                }
                EntityConnection.GetContext().Appreciation.AddObject(appreciation);

                EntityConnection.GetContext().SaveChanges();

                return appreciation;

            }
            catch (Exception ex)
            {

                throw ex;

            }
        }

        /// <summary>
        /// Méthode de suppression d'une Appreciation
        /// </summary>
        /// <param name="a">L'Appreciation à supprimer</param>
        public static void SupprimerAppreciation(Appreciation a)
        {
            try
            {
                EntityConnection.GetContext().Detach(a);
                Appreciation ap = AppreciationOPManager.GetAppreciationById(a.IdAppreciation);
                if(ap.EntityState == EntityState.Detached) {
                    EntityConnection.GetContext().Attach(ap);
                }
                EntityConnection.GetContext().Appreciation.DeleteObject(ap);
                EntityConnection.GetContext().SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
